<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<link rel="stylesheet" href="../assets/css/layui.css">
		<link rel="stylesheet" href="../assets/css/view.css" />

		<link rel="stylesheet" type="text/css" href="../js/bootstarp-3.3.7/css/bootstrap.min.css" />
		<!--<link rel="stylesheet" type="text/css" href="js/bootstarp-3.3.7/css/bo1otstrap.css"/>-->
		<script src="../vendor/jquery/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
		<!--<script src="../js/jquery-3.2.0.min.js" type="text/javascript" charset="utf-8"></script>-->
		<link rel="stylesheet" type="text/css" href="../js/bootstarp-table-1.11.1/bootstrap-table.min.css" />
		<script type="text/javascript" src="../js/bootstarp-3.3.7/js/bootstrap.js"></script>
		<script src="../js/bootstarp-table-1.11.1/bootstrap-table.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/bootstarp-table-1.11.1/bootstrap-table-zh-CN.min.js" type="text/javascript" charset="utf-8"></script>

		<title></title>
		<script>
			$(function() { //, headers: { 'x-requested-with': 'XMLHttpRequest' }
				$.ajaxSetup({
					crossDomain: true,
					xhrFields: {
						withCredentials: true
					}
				});
			});
		</script>
		<script type="text/javascript">
			var th = "";
			var str = "";
			var zzs = /^[1-9]*[1-9][0-9]*$/;
			var s = "";
			var nu = 0;
			var nu1 = 0;
			$(function() {

				$("#table1").bootstrapTable({

					type: "post",
					url: "http://127.0.0.1:8099/cgrk/findmed",
					pagination: true,
					pageSize: 10,
					clickToSelect: true,
					queryParams: queryParam,
					columns: [{
						checkbox: true,
						formatter: function(value, row, index) {
							return
						}

					}, {
						field: 'medicines_number',
						title: '药品编号',
						align: 'center',

					}, {
						field: 'medicines_name',
						title: '药品名称',
						align: 'center',

					}, {
						field: 'spec',
						title: '规格',
						align: 'center',

					}]

				})

				$.ajax({
					type: "post",
					url: "http://127.0.0.1:8099/cgrk/findsup",
					success: function(data) {
						for(i = 0; i < data.length; i++) {
							$("#supplier_id").append($("<option value='" + data[i].sid + "'>" + data[i].supplier_name + "</option>"))

						}
					}

				});

				$("#batches_of_inventory_number").val(Date.parse(new Date));

				$("#tjsh").mousedown(function() {
					if($("#supplier_id").val() == 0) {
						$("#sz").val(0);
						layer.msg('供应商不能为空!', {
							icon: 5,
							time: 1000
						});
					} else if($("#timed").val() == "") {
						$("#sz").val(0);
						layer.msg('制单日期不能为空!', {
							icon: 5,
							time: 1000
						});
					} else if($("#supplier_id").val() != 0 && $("#timed").val() != "") {
						$(".number").each(function(i) {
							var nums = $(".number:eq(" + i + ")").val()
							var nums1 = $(".num1:eq(" + i + ")").val()
							var nums2 = $(".num2:eq(" + i + ")").val()
							if(nums == 0 || nums1 == 0 || nums2 == 0) {
								$("#sz").val(0);
								layer.msg('数量,处方价或批发价不能为空!', {
									icon: 5,
									time: 1500
								});
							} else {

								$("#sz").val(1);
							}
						})
					}
					if($("#sz").val() == 1) {
						var tt = 1;
						
				//		alert("batches_of_inventory_date=" + $("#timed").val() + "&purchasing_agent_id=" + parseInt($("#purchasing_agent_id").val()) + "&batches_of_inventory_number=" + parseInt($("#batches_of_inventory_number").val()) + "&batches_of_inventory_total_money=" + $("#pfzje").val() + "&inventory_type=" + parseInt(tt) + "&supplier_id=" + parseInt($("#supplier_id").val()));
						$.ajax({
							type: "post",
							url: "http://127.0.0.1:8099/cgrk/addin",
							data: "batches_of_inventory_date=" + $("#timed").val() + "&purchasing_agent_id=" + parseInt($("#purchasing_agent_id").val()) + "&batches_of_inventory_number=" + parseInt($("#batches_of_inventory_number").val()) + "&batches_of_inventory_total_money=" + $("#pfzje").val() + "&inventory_type=" + parseInt(tt) + "&supplier_id=" + parseInt($("#supplier_id").val()),
							success:function (aa) {
//								alert(aa);
								$("#hdd").val(aa);
							}
							
						});
					}

				})
				setTimeout(

					$("#tjsh").mouseup(function() {
						
						var numbb = new Array();
						if($("#hdd").val() != 0) {

							$(".number").each(function(i) {
								//		alert(i);
								var sdfd = {
									"medicines_id": $(".hid:eq(" + i + ")").val(),
									"purchase_num": $(".number:eq(" + i + ")").val(),
									"purchase_unit": $(".unit:eq(" + i + ")").val(),
									"purchase_trade_price": $(".num1:eq(" + i + ")").val(),
									"purchase_prescription_price": $(".num2:eq(" + i + ")").val(),
									"purchase_trade_total_amount": $("#pfzje").val(),
									"purchase_prescription_total_amount": $("#cfzje").val(),
									"batches_of_inventory_id": $("#hdd").val()
								}
								numbb.push(sdfd);
							})
							$.ajax({
								type: "post",
								url: "http://127.0.0.1:8099/cgrk/addins",
								data: JSON.stringify(numbb),
								dataType: "json",
								contentType: "application/json",
								success: function(data) {
									if(data == null) {
										alert("提交失败")
									} else if(data != null) {
										alert("提交成功");
										window.location.reload();
									}

								}
							});
						}

					}),
					30000
				)
				//	
				//	

				$("#zjrk").mousedown(function() {

					if($("#supplier_id").val() == 0) {
						$("#sz").val(0);
						layer.msg('供应商不能为空!', {
							icon: 5,
							time: 1000
						});
					} else if($("#timed").val() == "") {
						$("#sz").val(0);
						layer.msg('制单日期不能为空!', {
							icon: 5,
							time: 1000
						});
					} else if($("#supplier_id").val() != 0 && $("#timed").val() != "") {
						$(".number").each(function(i) {
							var nums = $(".number:eq(" + i + ")").val()
							var nums1 = $(".num1:eq(" + i + ")").val()
							var nums2 = $(".num2:eq(" + i + ")").val()
							if(nums == 0 || nums1 == 0 || nums2 == 0) {
								$("#sz").val(0);
								layer.msg('数量,处方价或批发价不能为空!', {
									icon: 5,
									time: 1500
								});
							} else {

								$("#sz").val(1);
							}
						})
					}
					if($("#sz").val() == 1) {
						//	var fo = $("#for").serialize();
						var tt2 = 0;
						$.ajax({
							type: "post",
							url: "http://127.0.0.1:8099/cgrk/addin",
							data: "batches_of_inventory_date=" + $("#timed").val() + "&purchasing_agent_id=" + parseInt($("#purchasing_agent_id").val()) + "&batches_of_inventory_number=" + parseInt($("#batches_of_inventory_number").val()) + "&batches_of_inventory_total_money=" + $("#pfzje").val() + "&inventory_type=" + parseInt(tt2) + "&supplier_id=" + parseInt($("#supplier_id").val()),
							success: function(data) {
								if(data != null) {
									$("#hdd").val(data);
								}
							}
						});
					}

				})
				setTimeout(

					$("#zjrk").mouseup(function() {

						var numbb = new Array();
						if($("#hdd").val() != 0) {
							$(".number").each(function(i) {

								var sdfd = {
									"hmr_id": $(".hid:eq(" + i + ")").val(),
									"inventory_num": $(".number:eq(" + i + ")").val(),
									"inventory_unit": $(".unit:eq(" + i + ")").val(),
									"inventory_trade_price": $(".num1:eq(" + i + ")").val(),
									"inventory_prescription_price": $(".num2:eq(" + i + ")").val(),
									"inventory_trade_total_amount": $("#pfzje").val(),
									"inventory_prescription_total_amount": $("#cfzje").val()
								}
								numbb.push(sdfd);
							})
							$.ajax({
								type: "post",
								url: "http://127.0.0.1:8099/cgrk/addinslist",
								data: JSON.stringify(numbb),
								dataType: "json",
								contentType: "application/json",
								success: function(data) {
									if(data == null) {
										alert("提交失败")
									} else if(data != null) {
										alert("提交成功");
										window.location.reload();
									}

								}
							});
						}
						window.location.reload();
					}),
					30000
				)
			})

			function my() {
				$("#myModal").modal('show');
			}

			function myModalShow() {
				th = new Array();
				var getSelectRows = $("#table1").bootstrapTable('getSelections', function(row) {
					return row;
				});
				for(var i = 0; i < getSelectRows.length; i++) {

					th.push(getSelectRows[i].medicines_id);

				}

				str = th.join(",");

				$.ajax({
					type: "post",
					url: "http://127.0.0.1:8099/cgrk/findAll",
					data: "str=" + str,
					success: function(data) { 
						var tr = $("#table").find("tr").length - 1;
						for(i = 0; i < data.length; i++) {
							$("#table").append($("<tr  id='zz" + data[i].medicines_id + "'><td><input type='checkbox' name='text' value='" + data[i].medicines_id + "'/>" + (parseInt(tr) + i + 1) + " <input type='hidden' class='hid' value='" + data[i].medicines_id + "' /> </td><td><input type='text' style='width: 120px;' class='form-control name' value='" + data[i].medicines_name + "' readonly='readonly'></td><td><input type='text' style='width: 60px;' class='form-control spec' value='" + data[i].spec + "' readonly='readonly'></td><td><input type='text' style='width: 60px;' class='form-control number' id='n" + data[i].medicines_id + "' oninput='num(" + data[i].medicines_id + ")'></td><td id=><input type='text' style='width: 60px;' class='form-control unit' value='" + data[i].unit + "' readonly='readonly'></td><td><input type='text' style='width: 60px;' class='form-control num1' id='z" + data[i].medicines_id + "' oninput='z(" + data[i].medicines_id + ")' ></td><td><input type='text' style='width: 60px;' class='form-control num2' id='zh" + data[i].medicines_id + "' oninput='zh(" + data[i].medicines_id + ")' ></td><td><input type='text' style='border: 0px; outline: none; cursor: pointer; width: 120px;' readonly='readonly' class='h' id='h" + data[i].medicines_id + "' /></td><td><input type='text' style='border: 0px; outline: none; cursor: pointer; width: 120px;' readonly='readonly' class='hh' id='hh" + data[i].medicines_id + "' /></td><td><input type='button' class='form-control' onclick='sc(" + data[i].medicines_id + ")' value='删除'></td></tr>"))
						}
					}
				});
				$("#myModal").modal('hide');

			}

			//var c = 0;
			var a = 0;
			var b = 0;

			function num(i) {
				a = 0;
				b = 0;
				$(".number").each(function(i) {
					$(".h:eq(" + i + ")").val($(".number:eq(" + i + ")").val() * $(".num1:eq(" + i + ")").val())

					$(".hh:eq(" + i + ")").val($(".number:eq(" + i + ")").val() * $(".num2:eq(" + i + ")").val())
					a += parseInt($(".h:eq(" + i + ")").val());
					$("#pfzje").val(a);
					b += parseInt($(".hh:eq(" + i + ")").val());

					$("#cfzje").val(b);

				})

				if(zzs.test($("#n" + i + "").val())) {

				} else {
					$("#n" + i + "").val('');
				}

			}

			function z(i) {

				a = 0;

				$(".num1").each(function(i) {
					var nu = $(".number:eq(" + i + ")").val();
					var nu1 = $(".num1:eq(" + i + ")").val();
					$(".h:eq(" + i + ")").val(nu * nu1);
					a += parseInt($(".h:eq(" + i + ")").val());
					$("#pfzje").val(a);

				})

				if(zzs.test($("#num1" + i + "").val())) {

				} else {
					$("#num1" + i + "").val('');
				}

			}

			function zh(i) {

				b = 0;
				$(".num2").each(function(i) {

					$(".hh:eq(" + i + ")").val($(".number:eq(" + i + ")").val() * $(".num2:eq(" + i + ")").val())
					b += parseInt($(".hh:eq(" + i + ")").val());
					$("#cfzje").val(b);
				})
			}

			function sc(i) {

				var h = 0;
				$(".number").each(function(i) {

					h = $(".h:eq(" + i + ")").val()

				})
				$("#pfzje").val($("#pfzje").val() - h);
				$("#zz" + i + "").remove();
			}

			function but() {

				$("#table1").bootstrapTable("refresh");

			}

			function queryParam() {
				return {
					'medicines_name': $("#medicines_name").val(),
					'medicines_class': $("#medicines_class").val()

				}

			}

			function aqueryParam() {
				return {
					'str': str

				}

			}

			function check(ths) {

				if(ths.checked == true) {

					$("#tb :checkbox").prop("checked", true);

				} else {
					$("#tb :checkbox").prop("checked", false);
				}

			}

			function plsz() {

				var obj = document.getElementsByName('text'); //选择所有name="'test'"的对象，返回数组 
				//取到对象数组后，我们来循环检测它是不是被选中 
				s = [];
				for(var i = 0; i < obj.length; i++) {
					if(obj[i].checked) {
						s.push(obj[i].value);
					} //如果选中，将value添加到变量s中
				}
				//那么现在来检测s的值就知道选中的复选框的值了 
				//  alert(s==''?'你还没有选择任何内容！':s); 

				if(s != null && s != '') {

					$("#myModal1").modal('show');
				} else {
					alert("你还没有选择任何内容");
				}

			}

			function ysl() {

				if(zzs.test($("#ysl").val())) {

				} else {
					$("#ysl").val('');
				}
			}

			function ypf() {
				if(zzs.test($("#ypf").val())) {

				} else {
					$("#ypf").val('');
				}
			}

			function ycf() {
				if(zzs.test($("#ycf").val())) {

				} else {
					$("#ycf").val('');
				}
			}

			function d() {

				var obj = document.getElementsByName('text'); //选择所有name="'test'"的对象，返回数组 
				//取到对象数组后，我们来循环检测它是不是被选中 
				var s = [];
				var aa = 0;
				var bb = 0;
				for(var i = 0; i < obj.length; i++) {
					if(obj[i].checked) {

						$("#n" + obj[i].value + "").val($("#ysl").val());
						$("#z" + obj[i].value + "").val($("#ypf").val());
						$("#zh" + obj[i].value + "").val($("#ycf").val());
						$("#h" + obj[i].value + "").val($("#ysl").val() * $("#ypf").val());
						$("#hh" + obj[i].value + "").val($("#ysl").val() * $("#ycf").val());
						aa += $("#ysl").val() * $("#ypf").val();
						bb += $("#ysl").val() * $("#ycf").val();
						$("#pfzje").val(aa);
						$("#cfzje").val(bb);
					} //如果选中，将value添加到变量s中
				}

			}

			function qk() {
				$(".number").val('');
				$(".num1").val('');
				$(".num2").val('');
				$(".h").val('');
				$(".hh").val('');
				$("#pfzje").val('');
				$("#cfzje").val('');
			}
		</script>

	</head>
	<input type="hidden" id="hdd" value="" />
	<input type="hidden" id="sz" value="" />

	<div class="layui-content">
		<div class="layui-page-header">
			<div class="pagewrap">

				<body class="layui-view-body">

				
                
					<h2 class="title"></h2>

					<div>
						<button type="button" class="btn btn-primary" onclick='my()'>添加药品</button>
						<button class="btn btn-primary" onclick="plsz()"> 批量设置 </button>
						<!--<button class="btn btn-primary" onclick="tjsh()"> 提交审核 </button>-->
						<button class="btn btn-primary" id="tjsh"> 提交审核 </button>
						<button class="btn btn-primary" id="zjrk"> 直接入库 </button>
					</div>
			</div>
		</div>

		<div class="layui-row">
			<div class="layui-card">
				<div class="layui-card-body">
					<form id="for">
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label f">单据号:</label>
								<div class="layui-input-inline"> 
								<input type="text"  id="batches_of_inventory_number" class="form-control" readonly="readonly"   />
								</div>
								</div>	
							<div class="layui-inline">
								<label class="layui-form-label f">制单人:</label>
								<div class="layui-input-inline"> 
									<input type="text" class="form-control" id="purchasing_agent_id" readonly="readonly"  value="123" />
							
								</div>
								</div>	
							<div class="layui-inline">
								<label class="layui-form-label f">供应商:</label>
								<div class="layui-input-inline"> 
									<select class="form-control" name="supplier_id" id="supplier_id"><option value="0" >全部供应商</option></select>
								</div>
							</div>
							<div class="layui-inline">
									<label class="layui-form-label" style="width: 100px;">制作日期:</label>
									<div class="layui-input-inline"> 
										<input type="date" class="form-control" name="batches_of_inventory_date" id="timed">
									</div>
							</div>
						</div>	
					</form>
					<div style="text-align: right;">
						批发总金额：<input type='text' style='border: 0px; outline: none; cursor: pointer; width: 120px;' readonly='readonly' id="pfzje" /> 处方总金额：
						<input type='text' style='border: 0px; outline: none; cursor: pointer; width: 120px;' readonly='readonly' id="cfzje" />
						<button class="btn btn-danger" onclick="qk()">清空数值</button>
					</div>
					<form id="fr">
						<table id="table" style="text-align: center;" class="table table-hover">

							<tbody id="tb">
								<tr>
									<td>
										<input type="checkbox" onchange="check(this)" name="id" />序号
									</td>
									<td>
										药品名称
									</td>
									<td>
										规格
									</td>
									<td>
										数量
									</td>
									<td>
										单位
									</td>
									<td>
										批发价
									</td>
									<td>
										处方价
									</td>
									<td>
										批发额
									</td>
									<td>处方额</td>
									<td>操作</td>

								</tr>
								</thead>

						</table>
					</form>
				</div>
			</div>
		</div>
	</div>

	<!-- Button trigger modal -->

	<!-- Modal -->
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h4 class="modal-title" id="myModalLabel" style="text-align: center;">添加药品</h4>
				</div>
				<div class="modal-body">
					<div class="layui-row">
						<div class="layui-card">
							<div class="layui-card-body">
								<div class="form-box">
									<div class="layui-form layui-form-item">
										<div class="layui-inline">

											<div class="layui-form-mid">药品名称:</div>
											<div class="layui-input-inline" style="width: 100px;">
												<input type="text" id="medicines_name" autocomplete="off" class="layui-input">
											</div>
											<div class="layui-form-mid">药品分类:</div>
											<div class="layui-input-inline" style="width: 100px;">
												<select id="medicines_class">
													<option value="">-请选择-</option>
													<option value="中草药">中草药</option>
													<option value="中成药">中成药</option>
													<option value="西药">西药</option>
													<select>
											</div>

											<button type="button" class="layui-btn layui-btn-blue" onclick='but()'>查询</button>

										</div>
									</div>
								</div>
							</div>

						</div>
					</div>
					<table id="table1"></table>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</  >
        <button type="button" class="btn btn-primary" onclick='myModalShow()'>提交</button>
				</div>
			</div>
		</div>
	</div>

	<!-- Modal -->
	<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h4 class="modal-title" id="myModalLabel" style="text-align: center;">勾选要设置的药品后，再进行批量设置</h4>
				</div>
				<div class="modal-body">
					<div class="layui-form-item">
						<label class="layui-form-label">药数量</label>
						<div class="layui-input-block">
							<input type="text" name="title" required lay-verify="required" oninput="ysl()" id="ysl" placeholder="请输入药品数量" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">批发价</label>
						<div class="layui-input-block">
							<input type="text" name="title" required lay-verify="required" oninput="ypf()" id="ypf" placeholder="请输入批发价" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">处方价</label>
						<div class="layui-input-block">
							<input type="text" name="title" required lay-verify="required" oninput="ycf()" id="ycf" placeholder="请输入处方价" autocomplete="off" class="layui-input">
						</div>
					</div>

				</div>
				<div class="modal-footer">
					<div><input type="button" onclick="d()" class="layui-btn layui-btn-fluid" value="应用" /> </div>
				</div>
			</div>
		</div>
	</div>

	<script src="../assets/layui.all.js"></script>
	<script>
		var element = layui.element;
		var table = layui.table;
		var form = layui.form;
	</script>
	</body>

</html>